// 1. import express
const express = require('express');
const bodyParser = require('body-parser');
// 2. create app object 
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
// 3. create routing rules
// test get
app.get('/server', (request, response) => {
    // set response header
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Methods', 'GET,POST');
    // set response body
    response.send("hello, ajax GET!")
})

// test post 
// options for customized header
app.all('/server', (request, response) => {
    // set response header
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Methods', 'GET,POST');
    response.setHeader('Access-Control-Allow-Headers', '*')
    response.setHeader('content-type', 'application/json');
    console.log(request.body);
    response.send("hello, ajax POST!");
})

// test json handler
app.all('/json-server', (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Methods', 'GET,POST');
    response.setHeader('Access-Control-Allow-Headers', '*')
    response.setHeader('content-type', 'application/json');
    // prepare data 
    const data = {
        name: 'Tom',
        age: 12
    };
    // transfer object to string or byte buffer
    let str = JSON.stringify(data);
    response.send(str)
})

// test browser cache
app.get("/ie", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    response.send("Hello IE 3!")
})

// test timeout
app.get("/timeout", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    setTimeout(() => {
        response.send("timeout: 3s")
    }, 3000);
})

// test jquery
app.all("/jquery-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    response.setHeader('Access-Control-Allow-Headers', '*')
    const data = {
        'name': 'Tom',
        'age': 18
    }
    setTimeout(() => {
        response.send(JSON.stringify(data));
    }, 500);
})

// test axios
app.all("/axios-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    response.setHeader('Access-Control-Allow-Headers', '*')
    const data = {
        'name': 'Tom',
        'age': 18
    }
    setTimeout(() => {
        response.send(JSON.stringify(data));
    }, 500);
})

// test promise fetch
app.all("/fetch-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    response.setHeader('Access-Control-Allow-Headers', '*')
    const data = {
        'name': 'Tom',
        'age': 18
    }
    setTimeout(() => {
        response.send(JSON.stringify(data));
    }, 500);
})

// jsonp server # quickstart
app.all("/jsonp-server", (request, response) => {
    const data = {
        'name': 'Tom',
        'age': 18
    }
    let str = JSON.stringify(data);
    response.send(`handle(${str})`);
    // response.send("console.log('hello jsonp!')");

})

// jsonp server - username validation # jsonp
app.all("/jsonp-check-username", (request, response) => {
    const data = {
        exist: 1,
        msg: "username already exists!"
    }
    const str = JSON.stringify(data);
    response.send(`handle(${str})`);
})

// jquery jsonp server # jquery
app.all("/jquery-jsonp-server", (request, response) => {
    const data = {
        name:"Collins",
        city:["Beijing","Shanghai","Shenzhen"]
    }
    const str = JSON.stringify(data);
    // 接收callback参数
    let cb = request.query.callback;
    response.send(`${cb}(${str})`);
})

// cors server # cors
app.all("/cors-server", (request, response) => {
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Methods","GET,POST");
    response.setHeader("Access-Control-Allow-Headers","*");
    response.send("hello cors!");
})

// 4. listen port; start service
app.listen(8000, () => {
    console.log("service started, listening port 8000 ...")
});